package org.yray.tika;

import org.apache.tika.Tika;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.microsoft.ooxml.OOXMLParser;
import org.apache.tika.parser.pdf.PDFParser;
import org.apache.tika.parser.txt.TXTParser;
import org.apache.tika.sax.BodyContentHandler;

import java.io.File;
import java.io.FileInputStream;

/**
 * @author lyg
 * @date 2022/1/3 10:12
 */
public class TikaTest {
    public static void main(String[] args) {
        String fileName="D:\\BaiduNetdiskDownload\\AI经典书籍\\Python视觉实战项目52讲.pdf";
        System.out.println(getFileType(fileName));
        //getContext(fileName);
        parsePdf(fileName);
    }

    public static String getFileType(String fileName){
        try {
            File file = new File(fileName);
            Tika tika = new Tika();
            String fileType = tika.detect(file);
            if (fileType != null && fileType.contains("/")) {
                fileType = fileType.substring(fileType.indexOf("/") +1);
            }
            return fileType;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getContext(String fileName){

        try {
            File file = new File(fileName);
            Tika tika = new Tika();
            String filecontent = tika.parseToString(file);
            System.out.println("Extracted Content: " + filecontent);
            return filecontent;

        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";

    }

    public static String parsePdf(String fileName){

        try {

            BodyContentHandler handler = new BodyContentHandler(1000000000);

            Metadata metadata = new Metadata();
            FileInputStream inputstream = new FileInputStream(new File(fileName));
            ParseContext pcontext = new ParseContext();
            //parsing the document using PDF parser
            PDFParser pdfparser = new PDFParser();
            pdfparser.parse(inputstream, handler, metadata,pcontext);
            //getting the content of the document
            System.out.println("Contents of the PDF :" + handler.toString());
            // 元数据提取
            System.out.println("Metadata of the PDF:");

            String[] metadataNames = metadata.names();

            for(String name : metadataNames) {

                System.out.println(name+ " : " + metadata.get(name));

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


   /* public static String LanguageDetection() {

        try {

            Parser parser = new AutoDetectParser();

            BodyContentHandler handler = new BodyContentHandler();

            Metadata metadata = new Metadata();

            FileInputStream content = new FileInputStream(new File("D:\\111.zip"));

            //Parsing the given document

            parser.parse(content, handler, metadata, new ParseContext());
            LanguageDetector.getDefaultLanguageDetector().detect();
            LanguageIdentifier object = new LanguageIdentifier(handler.toString());

            System.out.println("Language name :" + object.getLanguage());

            return  object.getLanguage();

        } catch (Exception e) {

            e.printStackTrace();

        }

        return "";

    }*/

    public static String parseTxt(String fileName){

        try {

            BodyContentHandler handler = new BodyContentHandler();

            Metadata metadata = new Metadata();

            FileInputStream inputstream = new FileInputStream(new File(fileName));

            ParseContext pcontext=new ParseContext();

            //Text document parser

            TXTParser TexTParser = new TXTParser();

            TexTParser.parse(inputstream, handler, metadata,pcontext);

            System.out.println("Contents of the document:" + handler.toString());

            return handler.toString();

        } catch (Exception e) {

            e.printStackTrace();

        }

        return "";

    }

    public static String parseExcel(String fileName){

        try {

            BodyContentHandler handler = new BodyContentHandler();

            Metadata metadata = new Metadata();

            FileInputStream inputstream = new FileInputStream(new File(fileName));

            ParseContext pcontext=new ParseContext();

            OOXMLParser msofficeparser = new OOXMLParser ();

            msofficeparser.parse(inputstream, handler, metadata,pcontext);

            System.out.println("Contents of the document:" + handler.toString());

            return handler.toString();

        } catch (Exception e) {

            e.printStackTrace();

        }

        return "";

    }

}
